package com.danco.training.zaneuskaya.service.impl;


import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.repository.CountryDao;
import com.danco.training.zaneuskaya.service.CountryService;
import com.danco.training.zaneuskaya.service.exception.UserException;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("countryService")
@Transactional
public class CountryServiceImpl implements CountryService {

	private static final String COUNTRY_ALREADY_EXISTS = "Country already exists";

	private static final Logger logger = Logger
			.getLogger(CountryServiceImpl.class);

	@Autowired
	private CountryDao countryDao;

	public CountryDao getCountryDao() {
		return countryDao;
	}

	public void setCountryDao(CountryDao countryDao) {
		this.countryDao = countryDao;
	}

	@Override
	public void createEntity(Country t)  {
		try {
			if (countryDao.findCountryByName(t.getCountryName()) == null  ){
			
				countryDao.create(t);
			}else {
				throw new UserException(COUNTRY_ALREADY_EXISTS);
			}
			
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		} catch (UserException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public void deleteEntityById(Integer id) {
		try {
			Country country = countryDao.read(id);

			countryDao.delete(country);

		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public void updateEntity(Country t) {
		try {
			
			Country country = countryDao.read(t.getCountryId());
			country.setCities(t.getCities());
			country.setCountryName(t.getCountryName());
			countryDao.update(country);
				
			
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}

	}

	@Override
	public Country getEntityById(Integer id) {
		Country country = null;
		try {
			country = countryDao.read(id);
			if (country == null) {
				throw new NullPointerException("Country doesn't exist");
			}
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return country;
	}

	@Override
	public List<Country> getEntityList() {
		List<Country> countries = null;
		try {
			countries = countryDao.readAll();
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return countries;
	}

}
